package com.cn.web.controller.bc;

import com.alibaba.fastjson2.JSONObject;

import com.cn.bean.pojo.bc.K3bm;
import com.cn.config.config.Msg;
import com.cn.mapper.dao.db3.K3bmJdDao;
import com.cn.service.server.bc.GetK3bmServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class K3bmJdController {
    private final K3bmJdDao k3bmJdDao;
    private final GetK3bmServer getK3bmServer;

    @Autowired
    public K3bmJdController(K3bmJdDao k3bmJdDao,GetK3bmServer getK3bmServer) {
        this.k3bmJdDao = k3bmJdDao;
        this.getK3bmServer = getK3bmServer;
    }
    @RequestMapping("/update/Jd/K3bm")
    @ResponseBody
    public Msg getKh(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        List<K3bm> list = k3bmJdDao.selectK3bm();
        List<K3bm> list1 = getK3bmServer.selectK3bm();
        List<K3bm> k3NotInList1 = list.stream()
                .filter(kh -> list1.stream().noneMatch(kh1 -> Objects.equals(kh.getK3bm(), kh1.getK3bm())))
                .collect(Collectors.toList());
        for (K3bm k3bm : k3NotInList1) {
            int id = getK3bmServer.K3bmCount();
            int i = getK3bmServer.addK3bm(k3bm.getK3bm(), k3bm.getBmmc(), k3bm.getWlgg(), k3bm.getJldw(), k3bm.getFnumber(), id);
            if (i==0){
                msg.setCode(500);
                msg.setMessage("更新失败");
                return msg;
            }
        }
        msg.setCode(200);
        msg.setMessage("更新完成");
        return msg;
    }
}
